﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HRSystem.Entity;
using MySql.Data.MySqlClient;
using HRSystem.Utility;

namespace HRSystem.DBAccess
{
    public class ResultRecordAccess : Access
    {
        public List<ResultRecord> GetResultRecords(int baseInfoID)
        {
            string sql = "select * from result where person_id=" + baseInfoID;

            List<ResultRecord> list = new List<ResultRecord>();
            using (MySqlDataReader reader = MySqlUtil.GetDataReader(sql,null))
            {
                while (reader.Read())
                {
                    ResultRecord resultRecord = new ResultRecord();
                    resultRecord.ID = CheckIntNull(reader["id"]);
                    resultRecord.IsEmploy = Convert.ToBoolean(CheckIntNull(reader["is_employ"]));
                    resultRecord.EmployTime = CheckDatetimeNull(reader["employ_time"]);
                    resultRecord.EmployCompany = CheckNull(reader["employ_company"]);
                    resultRecord.EmployDepartment = CheckNull(reader["employ_department"]);
                    resultRecord.EmployPosition = CheckNull(reader["employ_position"]);
                    resultRecord.EmploySalar = CheckDoubleNull(reader["employ_salary"]);
                    resultRecord.BaseInfoID = CheckIntNull(reader["person_id"]);

                    list.Add(resultRecord);
                }
            }
            return list;
        }

        public bool AddOrUpdateResultRecord(ResultRecord resultRecord)
        {
            bool bNew = false;
            if (resultRecord.ID == 0)
            {
                bNew = true;
            }

            string sql = string.Empty;
            MySqlParameter[] myParams;

            if (bNew)
            {
                sql = @"insert result(is_employ,employ_time,employ_company,employ_department,employ_position,employ_salary,person_id) values(
                                ?is_employ,?employ_time,?employ_company,?employ_department,?employ_position,?employ_salary,?person_id)";
                myParams = new MySqlParameter[]{
                    new MySqlParameter("?is_employ",resultRecord.IsEmploy),
                    new MySqlParameter("?employ_time",resultRecord.EmployTime),
                    new MySqlParameter("?employ_company",resultRecord.EmployCompany),
                    new MySqlParameter("?employ_department",resultRecord.EmployDepartment),
                    new MySqlParameter("?employ_position",resultRecord.EmployPosition),
                    new MySqlParameter("?employ_salary",resultRecord.EmploySalar),
                    new MySqlParameter("person_id",resultRecord.BaseInfoID)                                               
                };
            }
            else
            {
                sql = @"update result set  is_employ=?is_employ,employ_time=?employ_time,employ_department=?employ_department,
                                employ_department=?employ_department,employ_company=?employ_company,employ_position=?employ_position,
                                employ_salary=?employ_salary,person_id=?person_id where id=?id";
                myParams = new MySqlParameter[]{
                    new MySqlParameter("?is_employ",resultRecord.IsEmploy),
                    new MySqlParameter("?employ_time",resultRecord.EmployTime),
                       new MySqlParameter("?employ_company",resultRecord.EmployCompany),
                    new MySqlParameter("?employ_department",resultRecord.EmployDepartment),
                    new MySqlParameter("?employ_position",resultRecord.EmployPosition),
                    new MySqlParameter("?employ_salary",resultRecord.EmploySalar),
                    new MySqlParameter("person_id",resultRecord.BaseInfoID),                                                 
                    new MySqlParameter("?id",resultRecord.ID)
                };
            }

            if (MySqlUtil.ExecuteNonQuery(sql,myParams) == -1)
            {
                return false;
            }

            resultRecord.ID = bNew ? GetLastID("result") : resultRecord.ID;
            return true;
        }
    }
}
